package com.mantru.iotpot.setting.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.mantru.iotpot.R;

import java.util.List;

public class CalibrateNunberKeyAdapter extends BaseAdapter{
    private Context context;
    private List<String> items;
    private OnCalibrateNumberClickListener listener;
    public interface OnCalibrateNumberClickListener {
        void onNumberClick(String number);
    }
    public CalibrateNunberKeyAdapter(Context context, List<String> items, OnCalibrateNumberClickListener listener) {
        this.context = context;
        this.items = items;
        this.listener = listener;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            // 关键点：必须关联 parent 作为 root
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.number_key_button, parent, false);
        }
        Button button = view.findViewById(R.id.number_key);
        String number = items.get(position);
        button.setText(number);
        //为所有按钮设置点击事件
        button.setOnClickListener(v -> {
            if (listener != null) {
                listener.onNumberClick(number);
                Log.d("numberkey", "adapter_number: " + number);
            }
        });
        return view;
    }
}
